//获取手机验证码
'use strict';
import React,{Component} from 'react';
import Header from '../common/header';
import styles from '../style/styles';
import Icon from 'react-native-vector-icons/FontAwesome';
import Register from './register';//注册
import ResetPassword from './resetPassword';//重置密码
import StringUtil from '../common/StringUtil';
import * as URLconstant from '../constant/URLconstant'
import Toast from '../common/ToastUtil';
import Util from '../common/util'
import FIcon from 'react-native-vector-icons/Ionicons';
import {
    Text,
    View,
    TextInput,
    TouchableOpacity,
    Image
} from 'react-native';
import LinearGradient from 'react-native-linear-gradient'
export default class resiger extends Component {
    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        //this.successPost=this.successPost.bind(this)
        this.successPost = this.successPost.bind(this)
        this.errorPost = this.errorPost.bind(this)
        this.state = {
            tel: ''
        };
    }

    next() {
        let tel = this.state.tel;
        if (StringUtil.validatemobile(tel) === 1) {

            if (this.props.type == 1) {
                let url = URLconstant.VERIFICATION_URL;
                let datakey = [];
                datakey['phone_mob'] = tel;
                Util.post(url, datakey, this.successPost, this.errorPost)
            } else {
                let url = URLconstant.VERIFICATION_URL;
                let datakey = [];
                datakey['phone_mob'] = tel;
                datakey['type'] = '2'
                Util.post(url, datakey, this.successPost, this.errorPost)
            }


        } else if (StringUtil.validatemobile(tel) === 2) {
            Toast.show('这个是邮箱')
            //let url=URLconstant.VERIFICATION_URL;
            //let datakey=[];
            //datakey['email']=tel;
        }
    }

    loginPop() {
        let that = this;
        this.props.navigator.pop();
    }

    successPost(datas) {
        let type = this.props.type;
        let tel = this.state.tel;
        if (datas.status == 0) {
            if (type == 1) {
                this.props.navigator.push({
                    component: Register,
                    passProps: {
                        phone_mob: tel,
                    }
                })
            } else {
                this.props.navigator.push({
                    component: ResetPassword,
                    passProps: {
                        phone_mob: tel,
                    }
                })
            }
        } else {
            Toast.show(datas.msg)
        }
        LOG(JSON.stringify(datas))
    }

    errorPost(err) {
        LOG(err)
    }

    confirmBtn() {
        let that = this;
        let tel = that.state.tel;
        if (tel != '') {
            return (
                <TouchableOpacity style={styles.login_btn} onPress={()=>this.next()}>
                    <Text style={styles.login_btn_txt}>下一步</Text>
                </TouchableOpacity>
            )
        } else {
            return (
                <View style={styles.no_login_btn}>
                    <Text style={styles.login_no_txt}>下一步</Text>
                </View>
            )
        }


    }
    _pop() {
        let that = this;
        that.props.navigator.pop();
    }
    render() {
        return (
            <View style={{flex:1}}>
                <LinearGradient
                    colors={['#5886ff', '#329cfa']}
                    start={{x: 0, y: 0}}
                    end={{x: 1, y: 1}}
                    locations={[0, 0.75]}
                >
                    <View style={styles.login_wrap}>
                        <View style={{height:90,justifyContent:'center',alignItems:'center',marginTop:60}}>
                            <Text style={{fontSize:36,color:'#fff'}}>手机号</Text>
                        </View>
                        <View style={styles.login_list}>
                            <Image
                                resizeMode="cover"
                                style={{height:35,width:35,marginLeft:5,marginRight:10}}
                                source={require('../imgs/phone.png')}/>
                            <TextInput
                                style={styles.login_input}
                                placeholder={'手机号'}
                                placeholderTextColor={'#fff'}
                                clearButtonMode={'while-editing'}
                                onChangeText={(text)=>this.setState({tel:text})}
                                underlineColorAndroid={'transparent'}
                            />
                        </View>
                        <View style={{height:65}}>
                            {this.confirmBtn()}
                        </View>
                    </View>
                </LinearGradient>
                <TouchableOpacity style={{height:40,position:'absolute',top:40,left:20}} onPress={()=>this._pop()}>
                    <FIcon name={'ios-arrow-back'} size={30} color="#fff"/>
                </TouchableOpacity>
            </View>
        )
    }
}